package com.nononsenseapps.feeder.db.entity

import androidx.room.*
import com.nononsenseapps.feeder.db.FEED
import com.nononsenseapps.feeder.db.FEED_RULE
import com.nononsenseapps.feeder.db.room.ID_UNSET
import com.nononsenseapps.feeder.db.room.RULE_FEED_ID

@Entity(tableName = FEED_RULE.tableName,
        indices = [Index(value = [FEED_RULE.COL_FEEDID])],
        foreignKeys = [ForeignKey(entity = Feed::class,
                parentColumns = [FEED.COL_ID],
                childColumns = [FEED_RULE.COL_FEEDID],
                onDelete = ForeignKey.CASCADE)])
data class FeedRule @Ignore constructor(
    @PrimaryKey(autoGenerate = true)
        @ColumnInfo(name = FEED_RULE.COL_ID) var id: Long = ID_UNSET,
    @ColumnInfo(name = FEED_RULE.COL_TITLE) var title: String = "",
    @ColumnInfo(name = FEED_RULE.COL_FEEDID) var feedId: Long? = null,
    @ColumnInfo(name = FEED_RULE.COL_AD_CHECK) var adClick: Boolean = false,// ad
    @ColumnInfo(name = FEED_RULE.COL_ABLE) var able: Boolean=true,//是否启用规则
    @ColumnInfo(name = FEED_RULE.COL_STYLE) var style: Int= FEED_ITEM_RULE_SOURCE.TITLE, //FEED_ITEM_RULE_SOURCE
    @ColumnInfo(name = FEED_RULE.COL_ACTION) var actionType: Int= FEED_ITEM_RULE_ACTION.MOVE_FEED, //FEED_ITEM_RULE_ACTION
    @ColumnInfo(name = FEED_RULE.COL_ACTION_FEEDID) var actionFeedId: Long = RULE_FEED_ID,
    @ColumnInfo(name = FEED_RULE.COL_RULE_VALUE) var ruleValue: String="",
    @ColumnInfo(name = FEED_RULE.COL_RULE_VALUE_IS_REGEX_PATTERN) var regexPattern: Boolean = false,//内容是不是正则
    @ColumnInfo(name = FEED_RULE.COL_ACTION_REPLACE_VALUE) var replaceValue: String=" "
){
    constructor() : this(id = ID_UNSET)

    override fun toString(): String {
        return title
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as FeedRule

        if (id != other.id) return false

        return true
    }

    override fun hashCode(): Int {
        return id.hashCode()
    }
}

object FEED_ITEM_RULE_SOURCE{
    const val TITLE = 0
    const val IMAGE_URL = 1
    const val DESCRIPTION = 2
    const val ENCLOSURE_LINK = 3
    const val AUTHOR = 4
    const val LINK = 5
    const val ITEM_TAGS = 6
}

object FEED_ITEM_RULE_ACTION{
    const val MOVE_FEED = 0
    const val REPLACE = 1
    const val MARK_READ = 3
    const val SCRIPT = 10
}

object RuleFactory{
    fun adImageRule(title: String,imageUrl:String,feedId: Long?): FeedRule = FeedRule(
        title = title ,
        style = FEED_ITEM_RULE_SOURCE.IMAGE_URL,
        ruleValue = imageUrl,
        actionType = FEED_ITEM_RULE_ACTION.REPLACE,
        adClick = true,
        feedId = feedId,
        replaceValue = " "
    )

    fun adTitleRule(title: String,str:String,feedId: Long?,moveFeedId: Long): FeedRule = FeedRule(
        title = title ,
        style = FEED_ITEM_RULE_SOURCE.TITLE,
        ruleValue = str,
        adClick = true,
        actionType = FEED_ITEM_RULE_ACTION.MOVE_FEED,
        feedId = feedId,
        actionFeedId = moveFeedId
    )
}